package com.myapp.ruyixing.ui;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Message;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.SafeUtils;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class ShopAddRemarkActivity extends BaseActivity implements
		OnClickListener {

	ImageView[] mLevelImageViews;
	private View mLoadingView;
	private EditText mInputText;
	private EditText mPriceText;

	int mScore;
	String mShopid;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			mLoadingView.setVisibility(View.GONE);

			Message message = (Message) o;
			if (message != null) {
				Toast.makeText(ShopAddRemarkActivity.this, message.msg,
						Toast.LENGTH_SHORT).show();
			}

			if (message.errno == 0) {
				finish();
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			mLoadingView.setVisibility(View.GONE);
			Toast.makeText(ShopAddRemarkActivity.this, throwable.getMessage(),
					Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_add_remark);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		((TitleBar) findViewById(R.id.title_bar))
				.setOnRightIconClickedListener(this);

		mLevelImageViews = new ImageView[5];
		mLevelImageViews[0] = (ImageView) findViewById(R.id.level1);
		mLevelImageViews[1] = (ImageView) findViewById(R.id.level2);
		mLevelImageViews[2] = (ImageView) findViewById(R.id.level3);
		mLevelImageViews[3] = (ImageView) findViewById(R.id.level4);
		mLevelImageViews[4] = (ImageView) findViewById(R.id.level5);
		setLevel(3);

		mInputText = (EditText) findViewById(R.id.remark);
		mPriceText = (EditText) findViewById(R.id.price);
		mLoadingView = findViewById(R.id.my_loading);

		if (!getIntent().getBooleanExtra("price", true)) {
			findViewById(R.id.layout_price).setVisibility(View.GONE);
		}

		for (int i = 0; i < mLevelImageViews.length; i++) {
			mLevelImageViews[i].setOnClickListener(this);
		}

		mShopid = getIntent().getStringExtra(ShopDetailActivity.INTENT_SHOP_ID);
		String shopName = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_NAME);

		if (TextUtils.isEmpty(shopName) == false) {
			((TitleBar) findViewById(R.id.title_bar)).setTitleText("添加点评\n"
					+ shopName);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.level1:
			setLevel(1);

			break;
		case R.id.level2:
			setLevel(2);

			break;
		case R.id.level3:
			setLevel(3);

			break;
		case R.id.level4:
			setLevel(4);

			break;
		case R.id.level5:
			setLevel(5);

			break;
		case R.id.img_icon_right:
			String sprice = mPriceText.getText().toString();
			if (TextUtils.isEmpty(sprice) == true
					&& getIntent().getBooleanExtra("price", true)) {
				Toast.makeText(ShopAddRemarkActivity.this, "价格不能为空",
						Toast.LENGTH_SHORT).show();
				return;
			}
			int price = SafeUtils.safeString2int(sprice);
			mLoadingView.setVisibility(View.VISIBLE);
			RYHttpClient.sendShopRemark(this, mHandler, mShopid, mInputText
					.getText().toString(), mScore, price);

			break;

		default:
			break;
		}
	}

	private void setLevel(int level) {
		mScore = level;
		for (int i = 0; i < level; i++) {
			mLevelImageViews[i].setImageResource(R.drawable.icon_add_remark_01);
		}
		for (int i = level; i < mLevelImageViews.length; i++) {
			mLevelImageViews[i].setImageResource(R.drawable.icon_add_remark_02);
		}
	}
}
